package com.jintoufs.web.action.sys;


import com.jintoufs.domain.hotel.HotelOrder;
import com.jintoufs.domain.scene.SceneOrder;
import com.jintoufs.domain.tour.TourRouteOrder;
import com.jintoufs.service.account.AccountDetailService;
import com.jintoufs.service.customer.CustomerService;
import com.jintoufs.service.divided.DividedService;
import com.jintoufs.service.hotel.HotelOrderService;
import com.jintoufs.service.scene.SceneOrderService;
import com.jintoufs.service.shop.ShopService;
import com.jintoufs.service.sys.DictionaryService;
import com.jintoufs.service.tour.TourRouteOrderService;
import com.jintoufs.util.date.DateUtils;
import com.jintoufs.util.valid.ValidUtil;
import com.jintoufs.uuid.SnowflakeIdWorker;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author cql
 */
public class AutoConfirm extends TimerTask {
    @Resource
    private SceneOrderService sceneOrderService;
    @Resource
    private HotelOrderService hotelOrderService;
    @Resource
    private TourRouteOrderService tourRouteOrderService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private SnowflakeIdWorker snowflakeIdWorker;
    @Resource
    private DividedService dividedService;


    public String getUUID() {
        return String.valueOf(snowflakeIdWorker.nextId());
    }

    @Override
    @Transactional
    public void run() {
        String days = dictionaryService.getDicByKeyNo("TIME_OUT_DAYS").get(0).getKeyValue();
        Date now = new Date();
        Map<String, Object> params = new HashMap<>();
        List<HotelOrder> hotelOrders = hotelOrderService.getNoConfirm(params);
        List<SceneOrder> sceneOrders = sceneOrderService.getNoConfirm(params);
        List<TourRouteOrder> tourRouteOrders = tourRouteOrderService.getNoConfirm(params);
        if (!ValidUtil.isEmpty(hotelOrders)) {
            for (HotelOrder hotelOrder : hotelOrders) {
                if (!"5".equals(hotelOrder.getStatus())) {
                    continue;
                }
                if (DateUtils.intervalDays(hotelOrder.getModifyTime(), now) >= Integer.valueOf(days)) {
                    dividedService.doDivided(hotelOrder,getUUID(),"hotel");
                }
            }
        }
        if (!ValidUtil.isEmpty(sceneOrders)) {
            for (SceneOrder sceneOrder : sceneOrders) {
                if (!"5".equals(sceneOrder.getStatus())) {
                    continue;
                }
                if (DateUtils.intervalDays(sceneOrder.getModifyTime(), now) >= Integer.valueOf(days)) {
                    dividedService.doDivided(sceneOrder,getUUID(),"scene");

                }
            }
        }
        if (!ValidUtil.isEmpty(tourRouteOrders)) {
            for (TourRouteOrder tourRouteOrder : tourRouteOrders) {
                if (!"5".equals(tourRouteOrder.getStatus())) {
                    continue;
                }
                if (DateUtils.intervalDays(tourRouteOrder.getModifyTime(), now) >= Integer.valueOf(days)) {
                    dividedService.doDivided(tourRouteOrder,getUUID(),"tour");
                }
            }
        }


    }
}